/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.taobao.remote.RemoteTaobaoProductServiceImpl
   Module Description   :

   Date Created      : 2010/12/13
   Original Author   : jeffma
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.taobao.remote;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.taobao.api.domain.Item;
import com.yaodian100.core.thread.service.ThreadService;
import com.yaodian100.ec.product.entity.Product;
import com.yaodian100.ec.product.entity.SpecificationInventory;
import com.yaodian100.ec.product.service.ProductService;
import com.yaodian100.ec.shelf.entity.Shelf;
import com.yaodian100.ec.shelf.service.ShelfService;
import com.yaodian100.ec.taobao.batch.BuildFeedCatsJob;
import com.yaodian100.ec.taobao.batch.BuildFeedItemJob;
import com.yaodian100.ec.taobao.service.TaobaoService;

/**
 * @author jeffma
 * 
 */
public class RemoteTaobaoProductServiceImpl implements RemoteTaobaoProductService {

	private final Logger logger = LoggerFactory.getLogger(RemoteTaobaoProductServiceImpl.class);

	@Autowired
	private ProductService productService;
	@Autowired
	private ShelfService shelfService;
	@Autowired
	private TaobaoService taobaoService;
	@Autowired
	private ThreadService threadService;

	private boolean debug = true;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.taobao.remote.RemoteTaobaoProductService#addProduct(java.lang.String)
	 */
	@Override
	public String addProduct(String productId) {
		Product product = productService.get(productId);
		Map<String, SpecificationInventory> siMap = productService.getSpecificationInventoryMap(productId);
		int qty = 0;

		String result = "";
		if (siMap.size() == 1) {
			for (SpecificationInventory si : siMap.values()) {
				qty += si.getInventory();
			}
			if (isDebug()) {
				qty = 1;
			}

			Shelf shelf = shelfService.getActiveShelfByProductId(productId);
			String numIid = taobaoService.itemsCustomGet(productId);
			if (StringUtils.isBlank(numIid)) {
				result = taobaoService.itemAdd(product, qty, shelf);
			} else {
				logger.error("exist product: numIid:" + numIid);
			}
		}

		return result;
	}

	/**
	 * @return the debug
	 */
	public boolean isDebug() {
		return debug;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.taobao.remote.RemoteTaobaoProductService#updateProduct(java.lang.String)
	 */
	@Override
	public boolean updateProduct(String productId) {
		Product product = productService.get(productId);
		Map<String, SpecificationInventory> siMap = productService.getSpecificationInventoryMap(productId);
		int qty = 0;

		boolean result = false;
		if (siMap.size() == 1) {
			for (SpecificationInventory si : siMap.values()) {
				qty += si.getInventory();
			}
			if (isDebug()) {
				qty = 1;
			}
			Shelf shelf = shelfService.getActiveShelfByProductId(productId);
			result = taobaoService.itemUpdate(product, qty, shelf);
		}

		return result;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.taobao.remote.RemoteTaobaoProductService#deleteProduct(java.lang.String)
	 */
	@Override
	public boolean deleteProduct(String productId) {
		return taobaoService.itemDelete(productId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.taobao.remote.RemoteTaobaoProductService#getProduct(java.lang.String)
	 */
	@Override
	public Item getProduct(String productId) {
		String iid = taobaoService.itemsCustomGet(productId);

		Item result = null;
		if (StringUtils.isNotBlank(iid) && StringUtils.isNumeric(iid)) {
			result = taobaoService.itemGet(Long.valueOf(iid));
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.taobao.remote.RemoteTaobaoProductService#buildFeed()
	 */
	@Override
	public boolean buildFeed(String percentage, int minSpecInv, boolean full) {
		try {
			threadService.executeInBackground(new BuildFeedCatsJob(taobaoService));

			threadService.executeInBackground(new BuildFeedItemJob(taobaoService, percentage, minSpecInv, full));
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}
}
